import {Component, OnInit, ViewChild} from '@angular/core';
import {HomeService} from 'src/app/services/home.service';
import {Banner} from 'src/app/services/data-types/common.type';
import {NzCarouselComponent} from 'ng-zorro-antd';
import {SingerService} from '../../services/singer.service';
import {ActivatedRoute} from '@angular/router';
import {map} from 'rxjs/internal/operators';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.less']
})
export class HomeComponent implements OnInit {
  banners: Banner[];
  hotTags = [];
  songSheetList = [];
  singerList = [];

  carouseActiveIndex = 0;

  @ViewChild(NzCarouselComponent, {static: true})
  private nzCarousel: NzCarouselComponent;

  onBeforeChange({to}) {
    this.carouseActiveIndex = to;
  }

  constructor(private homeServe: HomeService,
              private singerServe: SingerService,
              private route: ActivatedRoute) {
    this.route.data.pipe(map(result => result.homeDatas))
      .subscribe(([banners,hotTags,songSheetList,singerList]) => {
        this.banners = banners;
        this.hotTags = hotTags;
        this.songSheetList = songSheetList;
        this.singerList = singerList;
      });
    // this.getBanners();
    // this.getHotTags();
    // this.getSongSheetList();
    // this.getSinger();
  }

  ngOnInit() {
  }

  onChangeSlide(type: string) {
    this.nzCarousel[type]();
  }

  private getBanners() {
    this.homeServe.getBanners().subscribe(
      banners => {
        console.log('banners:', banners);
        this.banners = banners;
      }
    );
  }

  private getHotTags() {
    this.homeServe.getHotTags().subscribe(
      tags => {
        console.log('tags: ', tags);
        this.hotTags = tags;
      }
    );
  }

  private getSongSheetList() {
    this.homeServe.getSongSheetList().subscribe(
      sheet => {
        console.log('sheets: ', sheet);
        this.songSheetList = sheet;
      }
    );
  }

  private getSinger() {
    this.singerServe.getSinger().subscribe(
      singer => {
        console.log('singer: ', singer);
        this.singerList = singer;
      }
    );
  }
}
